package com.jiaoxue.tasknotes;

import android.arch.persistence.room.Room;
import android.content.Intent;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.callback.ItemDragAndSwipeCallback;
import com.chad.library.adapter.base.listener.OnItemSwipeListener;
import com.jiaoxue.tasknotes.Bean.TaskBean;
import com.jiaoxue.tasknotes.adapter.TaskAdapter;
import com.jiaoxue.tasknotes.addTask.AddTaskActivity;
import com.jiaoxue.tasknotes.db.AppDatabase;
import com.jiaoxue.tasknotes.db.TaskDao;
import com.jiaoxue.tasknotes.uis.RecycleViewDivider;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private List<TaskBean> mTaskBean;
    private RecyclerView recyclerView;
    private TaskAdapter adapter;
    private FloatingActionButton flaotbutton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.recytask);
        flaotbutton=findViewById(R.id.floatingActionButton);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        flaotbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, AddTaskActivity.class));

            }
        });
    }

//    public void addtask(View view) {
//        startActivity(new Intent(MainActivity.this, AddTaskActivity.class));
//    }

    @Override
    protected void onStart() {
        super.onStart();
        DataBaseAsyncTask dataBaseAsyncTask = new DataBaseAsyncTask();
        dataBaseAsyncTask.execute();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
//        DataBaseAsyncTask dataBaseAsyncTask=new DataBaseAsyncTask();
//        dataBaseAsyncTask.execute();
    }

    class DataBaseAsyncTask extends AsyncTask<Void, Void, List<TaskBean>> {
        TaskDao taskDao;

        @Override
        protected List<TaskBean> doInBackground(Void... voids) {
            return taskDao.getTasks();
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            taskDao = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tasks").build().taskDao();
        }

        @Override
        protected void onPostExecute(List<TaskBean> taskBeans) {
            super.onPostExecute(taskBeans);
            mTaskBean = taskBeans;
            Log.d("haomachi", ":" + mTaskBean.size());

             adapter = new TaskAdapter(taskBeans);
            recyclerView.setAdapter(adapter);

            ItemDragAndSwipeCallback itemDragAndSwipeCallback = new ItemDragAndSwipeCallback(adapter);
            ItemTouchHelper itemTouchHelper = new ItemTouchHelper(itemDragAndSwipeCallback);
            itemTouchHelper.attachToRecyclerView(recyclerView);
            //
            adapter.enableSwipeItem();
            adapter.setOnItemSwipeListener(onItemSwipeListener);
        }
    }

    class DeleteBaseAsyncTask extends AsyncTask<TaskBean, Void, Boolean> {
        TaskDao taskDao;


        @Override
        protected Boolean doInBackground(TaskBean... taskBeans) {
            taskDao.delect(taskBeans[0]);
            return true;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            taskDao = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "tasks").build().taskDao();
        }


    }

    OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
        int shanpos=0;
        @Override
        public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d("TAG", "" + pos);
           shanpos=pos;
        }

        @Override
        public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d("TAG", "" + pos);

         /*   if (mTaskBean.size() > 0) {
            }else {
                recyclerView.setAdapter(adapter);
            }*/
        }
        @Override
        public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
            Log.d("TAG", "onItemSwiped:" + pos);

            new DeleteBaseAsyncTask().execute(mTaskBean.get(pos));
        }

        @Override
        public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {

        }
    };
}


